using System;
using System.Collections.Generic;
using System.Text;

namespace Eve.Data
{
    public class Corporation : EveObject
    {
        public class InvestorShares
        {
            private Database db;
            private int investorId;
            private int shares;

            private EveObject investor;

            public EveObject Investor
            {
                get
                {
                    if (investor == null)
                        investor = EveData.GetObject(db, investorId);
                    return investor;
                }
            }
            public int Shares
            {
                get { return shares; }
            }

            internal InvestorShares(Database db, int id, int shares)
            {
                this.db = db;
                this.investorId = id;
                this.shares = shares;
            }
        }

        private int mainActivityId;
        private int secondaryActivityId;
        private char size;
        private char extent;
        private int solarSystemId;
        private List<InvestorShares> investors = new List<InvestorShares>();
        private int friendId;
        private int enemyId;
        private long publicShares;
        private int initialPrice;
        private double minSecurity;
        private bool scattered;
        private int fringe;
        private int corridor;
        private int hub;
        private int border;
        private int factionId;
        private double sizeFactor;
        private int stationCount;
        private int stationSystemCount;

        private CorporateActivity mainActivity;
        private CorporateActivity secondaryActivity;
        private SolarSystem solarSystem;
        private EveObject friend;
        private EveObject enemy;
        private Faction faction;

        private List<CorporationDivision> divisions;
        private List<CorporationResearchField> researchFields;
        private List<CorporationTrade> tradeItems;

        public CorporateActivity MainActivity
        {
            get
            {
                if (mainActivity == null)
                    mainActivity = new CorporateActivity(db, mainActivityId);
                return mainActivity;
            }
        }
        public CorporateActivity SecondaryActivity
        {
            get
            {
                if (secondaryActivity == null)
                    secondaryActivity = new CorporateActivity(db, secondaryActivityId);
                return secondaryActivity;
            }
        }
        public char Size
        {
            get { return size; }
        }
        public char Extent
        {
            get { return extent; }
        }
        public SolarSystem SolarSystem
        {
            get
            {
                if (solarSystem == null)
                    solarSystem = new SolarSystem(db, solarSystemId);
                return solarSystem;
            }
        }
        public List<InvestorShares> Investors
        {
            get { return investors; }
        }
        public EveObject Friend
        {
            get
            {
                if (friend == null)
                    friend = EveData.GetObject(db, friendId);
                return friend;
            }
        }
        public EveObject Enemy
        {
            get
            {
                if (enemy == null)
                    enemy = EveData.GetObject(db, enemyId);
                return enemy;
            }
        }
        public long PublicShares
        {
            get { return publicShares; }
        }
        public int InitialPrice
        {
            get { return initialPrice; }
        }
        public double MinimumSecurity
        {
            get { return minSecurity; }
        }
        public bool Scattered
        {
            get { return scattered; }
        }
        public int Fringe
        {
            get { return fringe; }
        }
        public int Corridor
        {
            get { return corridor; }
        }
        public int Hub
        {
            get { return hub; }
        }
        public int Border
        {
            get { return border; }
        }
        public Faction Faction
        {
            get
            {
                if (faction == null)
                    faction = new Faction(db, factionId);
                return faction;
            }
        }
        public double SizeFactor
        {
            get { return sizeFactor; }
        }
        public int StationCount
        {
            get { return stationCount; }
        }
        public int StationSystemCount
        {
            get { return stationSystemCount; }
        }
        public List<CorporationDivision> Divisions
        {
            get
            {
                if (divisions == null)
                {
                    divisions = new List<CorporationDivision>();
                    string query = "SELECT * FROM crpNPCCorporationDivisions WHERE corporationID=" + Id;
                    foreach (DbDataRow row in db.ExecuteQuery(query))
                        divisions.Add(new CorporationDivision(row, this));
                }
                return divisions;
            }
        }
        public List<CorporationResearchField> ResearchFields
        {
            get
            {
                if (researchFields == null)
                {
                    researchFields = new List<CorporationResearchField>();
                    string query = "SELECT * FROM crpNPCCorporationResearchFields WHERE corporationID=" + Id;
                    foreach (DbDataRow row in db.ExecuteQuery(query))
                        researchFields.Add(new CorporationResearchField(row, this));
                }
                return researchFields;
            }
        }
        public List<CorporationTrade> TradeItems
        {
            get
            {
                if (tradeItems == null)
                {
                    tradeItems = new List<CorporationTrade>();
                    string query = "SELECT * FROM crpNPCCorporationTrades WHERE corporationID=" + Id;
                    foreach (DbDataRow row in db.ExecuteQuery(query))
                        tradeItems.Add(new CorporationTrade(row, this));
                }
                return tradeItems;
            }
        }

        internal Corporation(DbDataRow dr)
            : base(dr.Database, EveObjectType.Corporation, dr["corporationID"])
        {
            Fill(dr);
        }
        internal Corporation(Database db, int id)
            : base(db, EveObjectType.Corporation, id)
        {
            Fill(db.ExecuteSingle("SELECT * FROM crpNPCCorporations WHERE corporationID=" + id));
        }
        private void Fill(DbDataRow dr)
        {
            mainActivityId = dr["mainActivityID"];
            secondaryActivityId = dr["secondaryActivityID"];
            size = dr["size"];
            extent = dr["extent"];
            solarSystemId = dr["solarSystemID"];
            for (int i = 1; i <= 4; i++)
            {
                int id = dr["investorID" + i];
                int shares = dr["investorShares" + i];
                if (id != 0) investors.Add(new InvestorShares(db, id, shares));
            }
            friendId = dr["friendID"];
            enemyId = dr["enemyID"];
            publicShares = dr["publicShares"];
            initialPrice = dr["initialPrice"];
            minSecurity = dr["minSecurity"];
            scattered = dr["scattered"];
            fringe = dr["fringe"];
            corridor = dr["corridor"];
            hub = dr["hub"];
            border = dr["border"];
            factionId = dr["factionID"];
            sizeFactor = dr["sizeFactor"];
            stationCount = dr["stationCount"];
            stationSystemCount = dr["stationSystemCount"];
        }
    }
}
